﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;

namespace ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 工序管理控制器
    /// </summary>
    [Route("[controller]/[action]")] // 使用属性路由, URL格式为: /Procedure/ActionName
    public class ProcedureController : Controller
    {
        private readonly IProcedureServer _procedureServer;

        /// <summary>
        /// 构造函数，通过依赖注入获取工序服务
        /// </summary>
        /// <param name="procedureServer">工序服务实例</param>
        public ProcedureController(IProcedureServer procedureServer)
        {
            _procedureServer = procedureServer;
        }

        /// <summary>
        /// 返回工序管理的首页视图
        /// </summary>
        /// <returns>ProcedureIndex.cshtml 视图</returns>
        [HttpGet]
        public IActionResult ProcedureIndex()
        {
            return View();
        }

        /// <summary>
        /// (API) 分页获取工序列表
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="isEnabled">是否启用</param>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>包含工序列表和总数的JsonResult</returns>
        [HttpGet]
        public async Task<JsonResult> GetPagedListAsync(string keyword = null, bool? isEnabled = null, int page = 1, int pageSize = 10)
        {
            var result = await _procedureServer.GetPagedListAsync(keyword, isEnabled, page, pageSize);
            return Json(result);
        }

        /// <summary>
        /// (API) 根据ID获取单个工序详情
        /// </summary>
        /// <param name="id">工序ID</param>
        /// <returns>单个工序DTO的JsonResult</returns>
        [HttpGet]
        public async Task<JsonResult> GetByIdAsync(int id)
        {
            var result = await _procedureServer.GetByIdAsync(id);
            return Json(result);
        }

        /// <summary>
        /// (API) 新增或编辑工序
        /// </summary>
        /// <param name="entity">从请求体中绑定的工序DTO</param>
        /// <returns>操作结果(true/false)的JsonResult</returns>
        [HttpPost]
        public async Task<JsonResult> SaveAsync([FromBody] ProcedureDTO entity)
        {
            var result = await _procedureServer.SaveAsync(entity);
            return Json(result);
        }

        /// <summary>
        /// (API) 根据ID删除单个工序
        /// </summary>
        /// <param name="id">工序ID</param>
        /// <returns>操作结果(true/false)的JsonResult</returns>
        [HttpPost]
        public async Task<JsonResult> DeleteAsync(int id)
        {
            var result = await _procedureServer.DeleteAsync(id);
            return Json(result);
        }

        /// <summary>
        /// (API) 批量删除工序
        /// </summary>
        /// <param name="ids">从请求体中绑定的工序ID列表</param>
        /// <returns>操作结果(true/false)的JsonResult</returns>
        [HttpPost]
        public async Task<JsonResult> DeleteBatchAsync([FromBody] List<int> ids)
        {
            var result = await _procedureServer.DeleteBatchAsync(ids);
            return Json(result);
        }
    }
}
